//
//  FindWatchViewController.m
//  SmartWatch
//
//  Created by zwj on 2022/11/24.
//

#import "FindWatchViewController.h"

@interface FindWatchViewController ()

@property(nonatomic,strong) NSMutableArray *searchArray;
@end

@implementation FindWatchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isHideNavigationBar = YES;
    self.searchArray = @[].mutableCopy;
    
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 220)];
    [topView az_setGradientBackgroundWithColors:@[[UIColor colorWithRGB:0xC4EFEC alpha:1.0],[UIColor colorWithRGB:0xC4EFEC alpha:0.75],[UIColor colorWithRGB:0xC4EFEC alpha:0.1]] locations:@[@0.0,@0.3,@1.0] startPoint:CGPointMake(0.5, 0) endPoint:CGPointMake(0.5, 1.0)];
    [self.view addSubview:topView];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:UIImageMake(@"icon_arrow_left") forState:UIControlStateNormal];
    backBtn.frame = CGRectMake(15, StatusBarHeight, 40, 40);
    backBtn.imageEdgeInsets = UIEdgeInsetsMake(10,10,10,10);
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    
    MyLinearLayout *root = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    root.myHorzMargin = 0;
    root.myHeight = kScreenHeight - (NavigationContentTop + 70);
    root.myTop = NavigationContentTop + 70;
    root.gravity = MyGravity_Horz_Center;
    [self.view addSubview:root];
    
    UILabel *statusLabel = [UILabel createLabelText:@"正在搜索可用设备" font:UIFontMake(20) textColor:COLOR333];
    statusLabel.mySize = kAutoSize;
    [root addSubview:statusLabel];
    
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
    backView.mySize = CGSizeMake(200, 200);
    backView.myTop = 40;
    [root addSubview:backView];
    
    
    for (int i = 0; i < 3; i++) {
        [self adddAnimation:backView num:i];
    }
    
    
    UIScrollView *scrollView = [UIScrollView new];
    scrollView.myHorzMargin = 0;
    scrollView.weight = 1;
    scrollView.myTop = 40;
    scrollView.showsVerticalScrollIndicator = NO;
    [root addSubview:scrollView];
    
    MyLinearLayout *listView = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    listView.myHorzMargin = 0;
    listView.myHeight = kAutoHeight;
    listView.subviewVSpace = 20;
    [scrollView addSubview:listView];
    
    NSArray *array = @[@"",@"",@"",@"",@"",@""];
    for (int i = 0; i < array.count; i++) {
        MyLinearLayout *item = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
        item.myTop = i == 0 ? 10 : 0;
        item.myHorzMargin = 15;
        item.gravity = MyGravity_Vert_Center;
        item.myHeight = 80;
        item.backgroundColor = COLOR_4;
        item.padding = UIEdgeInsetsMake(15, 15, 15, 15);
        item.subviewHSpace = 15;
        item.layer.cornerRadius = 10;
        item.layer.shadowColor = [UIColor colorWithRGB:0xDEEEEE alpha:0.8].CGColor;
        item.layer.shadowOffset = CGSizeMake(0,1);
        item.layer.shadowOpacity = 1;
        item.layer.shadowRadius = 8;
        [listView addSubview:item];
        
        UIImageView *logo = [[UIImageView alloc]initWithImage:UIImageMake(@"watch")];
        logo.mySize = CGSizeMake(24, 40);
        [item addSubview:logo];
        
        UILabel *name = [UILabel createLabelText:@"Watch 8" font:UIFontMake(18) textColor:COLOR1515];
        name.myHeight = 20;
        name.weight = 1;
        [item addSubview:name];
        
        UIButton *btn = [UIButton createButtonText:@"绑定" font:UIFontMake(15) textColor:THEME_COLOR];
        btn.mySize = CGSizeMake(80, 40);
        ViewCorner(btn, 20);
        ViewBorder(btn, 1, THEME_COLOR);
        [item addSubview:btn];
    }
    
    
    [[NJYBleService sharedInstance] scan];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:KSCANPERIPHERA_NOTIF object:nil];
    
    [[NJYBleService sharedInstance]getCentralManagerDidUpdateState:[NJYAsyncCallback create:nil success:^(id  _Nonnull result) {
        NSLog(@"BLE成功状态：%@",result);
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"BLE失败状态：%@",error);
    }]];
}

- (void)handleNotification:(NSNotification *)notifier {
   self.searchArray =  [NJYBleService sharedInstance].bleModals;
    NSLog(@"searchArray  %@",self.searchArray);
}

- (void)adddAnimation:(UIView *)backView num:(int)num{
    CGPoint centerPoint = CGPointMake(backView.height / 2, backView.width / 2);
    //使用贝塞尔画圆
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:1 startAngle:0 endAngle:2 * M_PI clockwise:YES];
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = backView.bounds;
    shapeLayer.fillColor = THEME_COLOR.CGColor;
    shapeLayer.opacity = 1.0;
    shapeLayer.path = path.CGPath;
    [backView.layer addSublayer:shapeLayer];
    
    //雷达圈圈大小的动画
    CABasicAnimation *basicAnimation = [CABasicAnimation animation];
    basicAnimation.keyPath = @"path";
    UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:1 startAngle:0 endAngle:2 * M_PI clockwise:YES];
    UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:100 startAngle:0 endAngle:2 * M_PI clockwise:YES];
    basicAnimation.fromValue = (__bridge id _Nullable)(path1.CGPath);
    basicAnimation.toValue = (__bridge id _Nullable)(path2.CGPath);
    basicAnimation.fillMode = kCAFillModeForwards;
    
    
    //雷达圈圈的透明度
    CABasicAnimation *opacityAnimation = [CABasicAnimation animation];
    opacityAnimation.keyPath = @"opacity";
    
    opacityAnimation.fromValue = @(1.0);
    opacityAnimation.toValue = @(0);
    opacityAnimation.fillMode = kCAFillModeForwards;
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = @[basicAnimation,opacityAnimation];
    
    //动画间隔时间  这里的值和创建的动画个数需要计算好，避免最后一轮动画结束了，第一个动画好没有结束，出现效果差
    group.duration = 3;
    //动画开始时间
    group.beginTime = CACurrentMediaTime() + (double)num;
    
    //循环次数最大（无尽）  HUGE
    group.repeatCount = HUGE;
    
    group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    //指定的时间段完成后,动画就自动的从层上移除
    group.removedOnCompletion = YES;
    //添加动画到layer
    [shapeLayer addAnimation:group forKey:nil];
}

@end
